using System.Web.UI.WebControls;
using System;
using System.ComponentModel;
namespace CodeStory.WebControls
{
	public interface IPropertyEditor
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="val"></param>
		void SetValue(object val);
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		object GetValue();
		/// <summary>
		/// 
		/// </summary>
		/// <param name="val"></param>
		void SetStandardValues(string val);
		/// <summary>
		/// 
		/// </summary>
		bool IsReadOnly { set; }
		/// <summary>
		/// 
		/// </summary>
		void Prepare();
	}

	[ToolboxItem(false)]
	public class TextBoxPropertyEditor : TextBox, IPropertyEditor
	{
		#region IPropertyEditor Members

		public void SetValue(object val) {
			Text = val.ToString();
		}


		public virtual object GetValue() {
			return Text;
		}


		public void SetStandardValues(string val) {
			throw new Exception("The method or operation is not implemented.");
		}

		public bool IsReadOnly {
			set { Enabled = !value; }
		}

		public virtual void Prepare() {
			
		}
		#endregion
	}
	[ToolboxItem(false)]
	public class DropDownPropertyEditor : DropDownList, IPropertyEditor
	{
		string valString;
		#region IPropertyEditor Members

		public void SetValue(object val) {
			valString = val.ToString();
			SetSelectedItem();
		}

		private void SetSelectedItem() {
			foreach (ListItem item in Items) {
				if (string.Equals(item.Value, valString, StringComparison.OrdinalIgnoreCase)) {
					item.Selected = true;
					break;
				}
			}
		}

		public virtual object GetValue() {
			return SelectedValue;
		}


		public void SetStandardValues(string val) {
			if (string.IsNullOrEmpty(val)) return;
			Items.Clear();
			string[] str = val.Split('$');
			foreach (string s in str) {
				ListItem item = new ListItem(s,s);
				Items.Add(item);
			}
		}

		public bool IsReadOnly {
			set { Enabled = !value; }
		}

		public virtual void Prepare() {
			
		}
		#endregion
	}
}